package com.efast.cafe.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class ConfigUtil {

	private static Properties ConfigProperties = new Properties();
	private static Properties ConfigPubProperties = new Properties();
	
	static {
	   try {
		     ConfigProperties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("application.properties"));
		     ConfigPubProperties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("application-public.properties"));
	   } catch (FileNotFoundException e) {
		       e.printStackTrace();
		} catch (IOException e) {
		    	 e.printStackTrace();
	    }
	 }
	
	public static Properties getConfigProperties(){
		return ConfigProperties;
	}
	
	public static Properties getConfigPubProperties(){
		return ConfigPubProperties;
	}
	
	
	
	public static String getUploadDir(){
		return getValue("cafe.upload.dir");
	}
	
	/**
	 * 获取当前系统标识
	 * @return
	 */
	public static String getSystemIndentifier(){
		return getValue("cafe.system.indentifier");
	}
	
	/**
	 * 获取当前系统版本
	 * @return
	 */
	public static String getSystemVersion(){
		return getValue("cafe.system.ver");
	}
	/**
	 * 获取当前系统所有的模块标识
	 * @return
	 */
	public static List<Map<String, String>> getSystemModules(){
		List<Map<String, String>> list = new ArrayList<>();
		String modules = getValue("cafe.system.modules");
		String[] modulesArray = modules.split(";");
		for(String m : modulesArray){
			Map<String, String> map = new HashMap<>();
			String[] ms = m.split(":");
			map.put("moduleName", ms[0]);
			map.put("contextPath", ms[1]);
			list.add(map);
		}
		return list;
	}
	
	public static String getValue(String key)
    {
		String v = ConfigProperties.getProperty(key);
	    return StringUtil.isNullOrBlank(v)?ConfigPubProperties.getProperty(key):v;
	}
	
	
}
